﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.GuiasDeEnvio;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using Microsoft.SharePoint;

namespace MS.InformationManagement.WP.DocumentCenter.GuiasDeEnvio
{
    public partial class GuiasDeEnvioUserControl : BaseControl, IGuiasDeEnvioView
    {
        #region fields

        GuiasDeEnvioPresenter _presenter;

        #endregion

        #region Properties

        public List<RadicadoSearchDTO> radicados { get; set; }
        public List<RadicadoSearchDTO> Guias { get; set; }
        public bool resultSave { get; set; }

        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            btnAdd.AddLoader();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Guias = new List<RadicadoSearchDTO>();
                _presenter = new GuiasDeEnvioPresenter(this);


                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void gvRadicados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Controls[0].Visible = false;
                    e.Row.Controls[1].Visible = false;
                    TextBox txt = (TextBox)e.Row.FindControl("txtIdGuia");
                    if (txt != null) 
                    {
                        txt.Required(ExceptionConstants.ERROR_DDL_OBLIGATORIO, "ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                        txt.MinMax(0, 10000, "Este campo no puede ser mayor a 10000", "ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                        txt.Numeric("99999", false);
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandleError(ex);
            }
        }


        protected void gvRadicados_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvRadicados_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow grd_Row in this.gvRadicados.Rows)
            {
                TextBox txt = (TextBox)grd_Row.FindControl("txtIdGuia");
                if (txt != null)
                {
                    if (!string.IsNullOrEmpty(txt.Text))
                        Guias.Add(new RadicadoSearchDTO() { Id = grd_Row.Cells[0].Text, IdList = grd_Row.Cells[1].Text, IdGuia = txt.Text });
                }
            }

            if (Guias != null)
                _presenter.SaveShippingGuide();
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            try
            {
                gvRadicados.DataSource = radicados;
                gvRadicados.DataBind();

                if (radicados.Count > 0)
                    btnAdd.Visible = true;
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        public void resultSaveSucced()
        {
            if (resultSave == true)
            {
                this.ShowAlertMessageAndRedirect("Las guías de envío fueron registradas", SPContext.Current.Web.Url  + "/_layouts/MS.InformationManagement.WP/GuiasDeEnvio.aspx");
                
            }
            else
            {
                this._ShowAlertAndClose("Ha ocurrido un error al ingresar su solicitud");
            }
        }
        
        #endregion

       


        #region Private Methods
        
        #endregion
    }
}
